﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class GSMATKGBData : BaseFunction
    {
        
        public GSMATKGBData()
            : base()
        {
           
        }
        public GSMATKGBData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string UNIT, COUNT, CO1, V1, CO2, V2, CO3, V3;
            UNIT = dwABPOS["UNIT"].ToString().Trim();
            COUNT = dwABPOS["COUNT"].ToString().Trim();
            CO1 = dwABPOS["CO1"].ToString().Trim();
            V1 = dwABPOS["V1"].ToString().Trim();
            CO2 = dwABPOS["CO2"].ToString().Trim();
            V2 = dwABPOS["V2"].ToString().Trim();
            CO3 = dwABPOS["CO3"].ToString().Trim();
            V3 = dwABPOS["V3"].ToString().Trim();
            try
            {
                OracleCommand or1 = this.OracleDB.CreateCommand();
          
                #region
                or1.CommandText = " SELECT A.GLLO,A.GLLB,A.GLLP,B.GPSPPART AS SPINLOT, "
                    + " B.GPFBPART AS DYELOT,A.GLPARTIE AS LOT,A.GLLBEHI AS BOXNO, "
                    + " A.GLBSTKG * 0.001 AS WEI,A.GLBSTNET * 0.001 AS NETWEI, "
                    + " A.GLBSTNET * 0.001 + A.GLTARA * 0.01 AS GROSSWEI, "
                    + " B.GPGART AS ARTICLE,B.GPGVAR AS VAR,C.GSHANDLNR AS DESCRIPTION, "
                    + " B.GPGFARBE AS COLOR,D.GSCBEZ AS COLORDES,C.GSRECHNUM AS UNIT, "
                    + " C.GSRECHNR * 0.0001 AS REALCOUNT, "
                    + " E.GSMRANT * 0.1 AS PORT1,Trim(E.GSMRBEZ) AS COMPOSITION1, "
                    + " F.GSMRANT * 0.1 AS PORT2,Trim(F.GSMRBEZ) AS COMPOSITION2, "
                    + " G.GSMRANT * 0.1 AS PORT3,Trim(G.GSMRBEZ) AS COMPOSITION3  "
                    + " FROM GLLG A,GPDAT B,GSDAT C,GSCOLOR D,GSMATK E, "
                    + " (SELECT GSMMANDANT,GSMART,GSMVAR,GSMRANT,GSMRBEZ FROM GSMATK  "
                    + " WHERE GSMPOS = '02') F, "
                    + " (SELECT GSMMANDANT,GSMART,GSMVAR,GSMRANT,GSMRBEZ FROM GSMATK  "
                    + " WHERE GSMPOS = '03') G "
                    + " WHERE A.GLMANDANT = B.GPMANDANT AND A.GLPARTIE = B.GPPARTIE "
                    + " AND A.GLPARTIEKZ = B.GPPARTIEKZ AND B.GPMANDANT = C.GSMANDANT "
                    + " AND B.GPGART = C.GSART AND B.GPGVAR = C.GSVAR  "
                    + " AND B.GPMANDANT = D.GSCMANDANT AND B.GPGART = D.GSCART "
                    + " AND B.GPGVAR = D.GSCVAR AND B.GPGFARBE = D.GSCFARBE "
                    + " AND B.GPMANDANT = E.GSMMANDANT AND B.GPGART = E.GSMART "
                    + " AND B.GPGVAR = E.GSMVAR AND E.GSMPOS = '01' AND A.GLLO = '01' "
                    + " AND (TRIM(C.GSRECHNUM) = '" + UNIT + "' OR '" + UNIT + "' = 'AAAAAA')"
                    + " AND (C.GSRECHNR * 0.0001 = '" + COUNT + "' OR '" + COUNT + "' = '999999') "
                    + " AND B.GPMANDANT = F.GSMMANDANT(+) AND B.GPGART = F.GSMART(+) "
                    + " AND B.GPGVAR = F.GSMVAR(+) "
                    + " AND B.GPMANDANT = G.GSMMANDANT(+) AND B.GPGART = G.GSMART(+) "
                    + " AND B.GPGVAR = G.GSMVAR(+) "
                    + " AND EXISTS (  "
                    + " SELECT '1' FROM GSMATK X "
                    + " WHERE X.GSMMANDANT = B.GPMANDANT AND X.GSMART = B.GPGART "
                    + " AND X.GSMVAR = B.GPGVAR AND ((TRIM(X.GSMRBEZ) = '" + CO1 + "' AND X.GSMRANT*0.1 >='" + V1 + "') "
                    + " OR '" + CO1 + "' ='AAAAAA' ))  "
                    + " AND EXISTS ( SELECT '1' FROM GSMATK Y "
                    + " WHERE Y.GSMMANDANT = B.GPMANDANT AND Y.GSMART = B.GPGART "
                    + " AND Y.GSMVAR = B.GPGVAR AND ((TRIM(Y.GSMRBEZ) = '" + CO2 + "' AND Y.GSMRANT*0.1 >='" + V2 + "') "
                    + " OR '" + CO2 + "' ='AAAAAA') ) AND EXISTS ( "
                    + " SELECT '1' FROM GSMATK Z "
                    + " WHERE Z.GSMMANDANT = B.GPMANDANT AND Z.GSMART = B.GPGART "
                    + " AND Z.GSMVAR = B.GPGVAR AND ((TRIM(Z.GSMRBEZ) = '" + CO3 + "' AND Z.GSMRANT*0.1 >='" + V3 + "') "
                    + " OR '" + CO3 + "' = 'AAAAAA' )  ) "
                    ;

                /*or1.Parameters.Add("UNIT", OracleType.NVarChar).Value = dwABPOS["UNIT"].ToString().Trim();
                or1.Parameters.Add("COUNT", OracleType.NVarChar).Value = dwABPOS["COUNT"].ToString().Trim();
                or1.Parameters.Add("CO1", OracleType.NVarChar).Value = dwABPOS["CO1"].ToString().Trim();
                or1.Parameters.Add("V1", OracleType.NVarChar).Value = dwABPOS["V1"].ToString().Trim();
                or1.Parameters.Add("CO2", OracleType.NVarChar).Value = dwABPOS["CO2"].ToString().Trim();
                or1.Parameters.Add("V2", OracleType.NVarChar).Value = dwABPOS["V2"].ToString().Trim();
                or1.Parameters.Add("CO3", OracleType.NVarChar).Value = dwABPOS["CO3"].ToString().Trim();
                or1.Parameters.Add("V3", OracleType.NVarChar).Value = dwABPOS["V3"].ToString().Trim();*/
                #endregion
                or1.CommandText = "select * from gsdat where rownum<10";
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "GSMATKGB";
                    foreach (DataTable dtTemp in setLocal.Tables)
                    {

                        setResult = ImportDataTable(setResult, dtTemp);
                        DataTable tableABPOS = setResult.Tables["GSMATKGB"];
                        /*
                        byte[] b;
                        string value;
                        string ColumnName;
                        foreach (DataRow dwTemp in setLocal.Tables["GSMATKGB"].Rows)
                        {
                            ColumnName = "ARTICLE";
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[ColumnName].ToString());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dwTemp[ColumnName] = value;

                            ColumnName = "COLORDES";
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[ColumnName].ToString());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dwTemp[ColumnName] = value;

                            ColumnName = "SUPPLIER";
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[ColumnName].ToString());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dwTemp[ColumnName] = value;

                            ColumnName = "FACTORY";
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[ColumnName].ToString());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dwTemp[ColumnName] = value;


                        }*/
                    }

                    set4Return.Merge(setResult);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        public DataSet BuildGBBEW(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}